package org.NooLab.itexx.co.targets;

import java.io.IOException;

import org.NooLab.itexx.ITexx;
import org.NooLab.itexx.ITexxFactory;
import org.NooLab.itexx.app.up.TxStartup;
import org.NooLab.itexx.co.CoWorker;
import org.NooLab.itexx.co.CoWorkerIntf;
import org.NooLab.itexx.co.CoWorkers;
import org.NooLab.itexx.co.ITexxModuleAppIntf;
import org.NooLab.utilities.net.GUID;




/**
 * 
 * 
 * 
 * 
 *
 */
public class CoSomClients implements ITexxModuleAppIntf{

	
	ITexxFactory itxFactory ;
	
	CoWorker processDef ;
	CoWorkers coWorkers;
	ITexx itexx ;
	
	
	public CoSomClients(ITexxFactory factory, ITexx itx) {
	 
		itxFactory = factory ;
		
		itexx = itx ;
		coWorkers = itexx.getCoWorkersContainer();
	}

	public void configure(int dedicatedPort, int maxNumberOfConcurrents) throws IOException {
		 
		String path ;
		
		processDef = coWorkers.getCoWorker(itexx) ;
		
		processDef.setJarFilename("NooLabSomClients.jar") ;
		processDef.setMainClass("org.NooLab.somclients.SomClientsApp") ;
		
		int ccn = coWorkers.GetConcurrentCountOf( processDef );
		if (ccn>maxNumberOfConcurrents){
			// in particular, we will not add this process definition to 
			// the items list in CoWorkers, and also we will not create an
			// instance of JavaProcessBuilder (jvm)
			return;
		}

		
		processDef.setJarFilePath("/bin"); // relative to system root   
		processDef.setExtLibrariesPath("/bin/lib") ;
		
		
		
							path = TxStartup.getProjectBasePath();
							path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(), "app/SomClients");
        processDef.addArgument( "-homedir: "+path); 
        processDef.addArgument( "-cfg:somclients-config.xml");
        processDef.addArgument( "-serviceports: in7202, out7201");
        processDef.addArgument( "-systemports: in7212, out7211");

        					String instanceGuid = GUID.randomvalue() ;
		processDef.setInstanceGuid(instanceGuid);
		processDef.addArgument( "-waitfor:"+instanceGuid); 
		processDef.addArgument( "-name:_somclient_"); // we use a small decoration

		   				int port = coWorkers.getNextPort( instanceGuid );
		   				String portStr = "in-"+port+",out-"+CoWorkerIntf._mainServerPort;
		   				processDef.setProposedPort(port); // this we need to send a release during startup
        processDef.addArgument( "-comm:"+ portStr);


        processDef.setName(this.getClass().getSimpleName());
        
		processDef.create() ;
		coWorkers.add(processDef) ;
	}
	

	@Override
	public void launch() throws Exception {
	 
		
	}

	@Override
	public CoWorker getProcessDef() {
		 
		return processDef;
	}

}
